using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Recruiters_postjob : System.Web.UI.Page
{
    string connectionString = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString =
                         WebConfigurationManager.ConnectionStrings[
                           "RecruitVietnam"].ConnectionString;
        if (Session.Contents["rcuserid"] == null)
        {
            Response.Redirect("login.aspx");
        }
        if(Session["rcactivate"].ToString() =="False")
            {
                btnSave.Enabled = false;
                btnActivate.Enabled = false;
                literalError.Text = "<font color=red><img border=0 src='/icons/warning.gif'>You can not use RCV Jobs and Viewing services, <br>because this account is not approved by management team.</font>";
            }
        if(!IsPostBack)
        {
            LoadDataToDropDownList();
            string jobID = Request.QueryString.Get("JobID");
            postAndUpdateDate.Visible = false;
            
            if(jobID != null && jobID != "")
            {
                literalID.Text = jobID;
                if(!EditJob(jobID))
                {
                    
                    btnActivate.Visible = false;
                    btnPreView.Visible = false;
                    literalError.Text = "Invalid job.";
                }
                else
                {

                    postAndUpdateDate.Visible = true;
                    lblTitle.Text = "Update Job";
                    btnSave.Text = "Update";
                    btnActivate.Visible = 
                    btnPreView.Visible = true;
                    literalError.Text = "";
                }
             } 
             else
             {
                if(IsEnablePostJobs())
                    LoadRangeOfDate(ddlCloseDate, 30,"");  
                else
                    Response.Redirect("postjob.aspx");
             }
        }

    }
    bool IsEnablePostJobs()
    {        
        SqlServerProvider cls = new 
        SqlServerProvider(connectionString);
        cls.CommandText = 
        "select AvailableForPosting "+
        " from tblRCAccounts where "+
        " AvailableForPosting>0 and "+
        " RecruiterID =" + 
        Session.Contents["rcuserid"].ToString();
        DataRow dataRow = cls.GetDataRow();
        return (dataRow != null);
        
    }
    void LoadRangeOfDate(DropDownList dropDownList, int days, string selectDate)
    {
        if(days>0)
        {
           for (int day = 1; day <= days; day++)
               {
                    dropDownList.Items.Add(new ListItem (DateTime.Now.AddDays(day).ToString("MM/dd/yyyy"), DateTime.Now.AddDays(day).ToString("MM/dd/yyyy")));
                    
               }
           if (selectDate != "")
               dropDownList.SelectedValue = selectDate;
           else
               dropDownList.SelectedValue = DateTime.Now.AddDays(days).ToString("MM/dd/yyyy");
        }
        else
           dropDownList.Items.Add(selectDate);
    }
    void LoadDataToDropDownList()
    {
        DropDownListHelper helper =
                new DropDownListHelper();
        Common cls = new Common(connectionString);
        DataSet dataSet = cls.GetDataSet(
        "select * from tblJobCategories; " +
            "Select * from tblJobIndustries;" +
            "select * from tblProvinces;" +
            "select * from tblJSCertificates;" +
            "select * from tblExperienceLevel;" +
            "select * from tblWorkingTypes;" +
            "select * from tblRCFolders "+
            " where RecruiterID=" + 
            Session.Contents["rcuserid"].ToString());
        DropDownList[] DropDownLists =
        new DropDownList[7] { ddlCategory, 
                ddlIndustry, ddlProvince, 
                ddlCertificate, ddlLevel,
                ddlWorkingType, ddlFolder};
        helper.FillDataTableToDropDownList(
        dataSet, DropDownLists,
        new string[7] { "JobCategoryName", 
                "JobIndustryName",
                "ProvinceName", 
                "CertificateName",
                "ExperienceLevelName",
                "WorkingTypeName", "FolderName"},
        new string[7] { "JobCategoryID", 
                "JobIndustryID", "ProvinceID", 
                "CertificateID", "ExperienceLevelID",
                "WorkingTypeID", "FolderID"},
        new string[7] { "", "", "", "", "", "", "" });
    }
    bool EditJob(string jobID)
    {
        bool successfull = false;
        SqlServerProvider cls = new SqlServerProvider(connectionString);
        cls.CommandText = "SPViewPostJob";
        cls.ParameterCollection = new string[2] { 
        "@JobID", "@RecruiterID" };
        cls.ValueCollection = new string[2] { jobID, 
        Convert.ToString(Session["rcuserid"]) };
        cls.CommandType = CommandType.StoredProcedure;
        DataRow dataRow = cls.GetDataRow();
        if(dataRow != null)
        {
            literalPostDate.Text  = dataRow["PostDate"].ToString();
            literalUpdDate.Text = dataRow["UpdateDate"].ToString();
            txtNo.Text = dataRow["JobNo"].ToString();
            LoadRangeOfDate(ddlCloseDate, Convert.ToInt32(dataRow["RemainDays"]), dataRow["ClosedDate"].ToString());
             
            txtTitle.Text = dataRow["JobTitle"].ToString();
            txtSummary.Text = dataRow["JobSummary"].ToString();
            ddlCategory.SelectedValue = dataRow["JobCategoryID"].ToString();
            ddlIndustry.SelectedValue = dataRow["JobIndustryID"].ToString();
            ddlCertificate.SelectedValue = dataRow["CertificateID"].ToString();
            ddlLevel.SelectedValue = dataRow["ExperienceLevelID"].ToString();
            txtYearExperience.Text = dataRow["YearExperience"].ToString();
            ddlWorkingType.SelectedValue = dataRow["WorkingTypeID"].ToString();
            txtContactEmail.Text = dataRow["ContactEmail"].ToString();
            txtContactPerson.Text = dataRow["ContactPerson"].ToString();
            txtContactTel.Text = dataRow["ContactTel"].ToString();
            txtNumber.Text = dataRow["RequiredNumber"].ToString();
            txtAge.Text = dataRow["RangeOfAge"].ToString();
            txtRecruitment.Text = dataRow["RecruitmentType"].ToString();
            chkShowLogo.Checked = (dataRow["CompanyLogo"].ToString() == "True" ? true : false);
            txtSalaryFrom.Text = dataRow["SalaryFrom"].ToString();
            txtSalaryTo.Text = dataRow["SalaryTo"].ToString();
            ddlCurrency.SelectedValue = dataRow["Currency"].ToString();
            chkNegotive.Checked = (dataRow["SalaryNegotive"].ToString() == "True" ? true : false);
            chkShowSalary.Checked = (dataRow["ShowSalary"].ToString() == "True" ? true : false);
            ddlProvince.SelectedValue = dataRow["ProvinceID"].ToString();
            txtLocation.Text = dataRow["WorkLocation"].ToString();
            txtSkills.Text = dataRow["JobSkills"].ToString();
            txtAdv.Text = dataRow["AdvName"].ToString();
            chkApplyOnline.Checked = (dataRow["EnableApplyOnline"].ToString() == "True" ? true : false);
            txtURL.Text = dataRow["OnlyApplyURL"].ToString();
            ddlFolder.SelectedValue = dataRow["FolderID"].ToString();
            ddlTemplate.SelectedValue = dataRow["TemplateID"].ToString();
            btnActivate.Text = (dataRow["Activate"].ToString() == "True" ? "Disactivate" : "Activate");
            litDayLeft.Text = "Left days: " + dataRow["RemainDays"].ToString();
            successfull = true;
        }
        return successfull;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        RecruitVietnam recruitVietnam
                        = new RecruitVietnam();
        recruitVietnam.ConnectionString = connectionString;
        if (recruitVietnam.AddNewOrUpdateJobPosting(
            (literalID.Text == "" ? "0" : literalID.Text),
            Convert.ToString(Session["rcuserid"]),
            txtNo.Text, ddlCloseDate.SelectedValue,
            txtTitle.Text, txtSummary.Text, 
            ddlCategory.SelectedValue,
            ddlIndustry.SelectedValue,
            ddlCertificate.SelectedValue,
            ddlLevel.SelectedValue,
            txtYearExperience.Text,
            ddlWorkingType.SelectedValue,
            txtContactEmail.Text,
            txtContactPerson.Text,
            txtContactTel.Text,            
            txtNumber.Text, txtAge.Text,
            txtRecruitment.Text,
            chkShowLogo.Checked,
            txtSalaryFrom.Text, txtSalaryTo.Text,
            ddlCurrency.SelectedValue,
            chkNegotive.Checked,
            chkShowSalary.Checked,
            ddlProvince.SelectedValue,
            txtLocation.Text, txtSkills.Text,
            txtAdv.Text, chkApplyOnline.Checked,
            txtURL.Text, ddlFolder.SelectedValue,
            ddlTemplate.SelectedValue))
                Response.Redirect("jobmanager.aspx");
        else
            literalError.Text = recruitVietnam.ErrorMessage;
    }
    protected void btnPreView_Click(object sender, EventArgs e)
    {
        Response.Redirect("jobdetails.aspx?JobID=" + literalID.Text);
    }
}
